package de.gatav.projekt;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;

/*
 * Puzzleteile
 */

public class Tile {
	
	private int id; // Ziel des Puzzleteils im Grid (Bsp. id=1 => Zielgridfeld = 1)
	private int currentID; // Aktuelle Position im Grid
	private float [] currentGrid; // Aktuelle Koordinaten des Puzzleteil
	
	private ImageView view;
	
	public Tile(ImageView v ,int id) {
		
		this.id = id;
		this.view = v;
		
		this.view.setOnClickListener(new OnClickListener() {
			
			
			@Override
			/*
			 * Gibt dem Puzzleteil die Eigenschaft sich bei einem Klick zu bewegen
			 */
			public void onClick(View v) {
				
				AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();
				ObjectAnimator pushRight;
				ObjectAnimator pushLeft;
				ObjectAnimator pushDown;
				ObjectAnimator pushUp;
				
				/*
				 * nach rechts bewegen
				 */
				pushRight = ObjectAnimator.ofFloat(v, "x", v.getX()+160f).setDuration(140);
				pushRight.addListener(new AnimatorListener() {
					
					@Override
					public void onAnimationStart(Animator animation) {}
					
					@Override
					public void onAnimationRepeat(Animator animation) {}
					
					@Override
					public void onAnimationEnd(Animator animation) {
		
						MainActivity.isSolved();
					}

					@Override
					public void onAnimationCancel(Animator animation) {}
				});
				pushRight.setInterpolator(interpolator);
				
				/*
				 * nach links bewegen
				 */
				pushLeft = ObjectAnimator.ofFloat(v, "x", v.getX()-160f).setDuration(140);
				pushLeft.addListener(new AnimatorListener() {
					
					@Override
					public void onAnimationStart(Animator animation) {}
					
					@Override
					public void onAnimationRepeat(Animator animation) {}
					
					@Override
					public void onAnimationEnd(Animator animation) {
						MainActivity.isSolved();
					}

					@Override
					public void onAnimationCancel(Animator animation) {}
				});
				pushLeft.setInterpolator(interpolator);
				
				/*
				 * nach unten bewegen
				 */
				pushDown = ObjectAnimator.ofFloat(v, "y", v.getY()+160f).setDuration(140);
				pushDown.addListener(new AnimatorListener() {
					
					@Override
					public void onAnimationStart(Animator animation) {}
					
					@Override
					public void onAnimationRepeat(Animator animation) {}
					
					@Override
					public void onAnimationEnd(Animator animation) {

						MainActivity.isSolved();
					}

					@Override
					public void onAnimationCancel(Animator animation) {}
				});
				pushDown.setInterpolator(interpolator);
				
				/*
				 * nach oben bewegen
				 */
				pushUp = ObjectAnimator.ofFloat(v, "y", v.getY()-160f).setDuration(140);
				pushUp.addListener(new AnimatorListener() {
					
					@Override
					public void onAnimationStart(Animator animation) {}
					
					@Override
					public void onAnimationRepeat(Animator animation) {}
					
					@Override
					public void onAnimationEnd(Animator animation) {
						
						MainActivity.isSolved();
					}

					@Override
					public void onAnimationCancel(Animator animation) {}
				});
				pushUp.setInterpolator(interpolator);
				
				
				MainActivity.GridsInUse(MainActivity.tileArray);
				getCurrentID();
				getCurrentGrid();
				
				
				if(currentID == 1){
					if(MainActivity.gridlist[1][2] == 0){
						pushRight.start();
					}
					else if(MainActivity.gridlist[3][2] == 0){
						pushDown.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");
						
					}
				}
				if(currentID == 2){
					if(MainActivity.gridlist[0][2] == 0){
						pushLeft.start();
					}
					else if(MainActivity.gridlist[2][2] == 0){
						pushRight.start();
					}
					else if(MainActivity.gridlist[4][2] == 0){
						pushDown.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");
					}
				}
				if(currentID == 3){
					if(MainActivity.gridlist[1][2] == 0){
						pushLeft.start();
					}
					else if(MainActivity.gridlist[5][2] == 0){
						pushDown.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");
					}
				}
				if(currentID == 4){
					if(MainActivity.gridlist[0][2] == 0){
						pushUp.start();
					}
					else if(MainActivity.gridlist[4][2] == 0){
						pushRight.start();
					}
					else if(MainActivity.gridlist[6][2] == 0){
						pushDown.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");
					}
				}
				if(currentID == 5){
					if(MainActivity.gridlist[1][2] == 0){
						pushUp.start();
					}
					else if(MainActivity.gridlist[3][2] == 0){
						pushLeft.start();
					}
					else if(MainActivity.gridlist[7][2] == 0){
						pushDown.start();
					}
					else if(MainActivity.gridlist[5][2] == 0){
						pushRight.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");
					}
				}
				if(currentID == 6){
					if(MainActivity.gridlist[2][2] == 0){
						pushUp.start();
					}
					else if(MainActivity.gridlist[4][2] == 0){
						pushLeft.start();
					}
					else if(MainActivity.gridlist[8][2] == 0){
						pushDown.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");
					}
				}
				if(currentID == 7){
					if(MainActivity.gridlist[3][2] == 0){
						pushUp.start();
					}
					else if(MainActivity.gridlist[7][2] == 0){
						pushRight.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");		
					}
				}
				if(currentID == 8){
					if(MainActivity.gridlist[6][2] == 0){
						pushLeft.start();
					}
					else if(MainActivity.gridlist[8][2] == 0){
						pushRight.start();
					}
					else if(MainActivity.gridlist[4][2] == 0){
						pushUp.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");
					}
				}
				if(currentID == 9){
					if(MainActivity.gridlist[7][2] == 0){
						pushLeft.start();
					}
					else if(MainActivity.gridlist[5][2] == 0){
						pushUp.start();
					}
					else{
						//Log.v("puzzleDebug", "No empty neighboured grids!");	
					}
				}	
			}
		});
	}
	
	/*
	 * Getter und Setter
	 */
	public int getId(){
		return id;
	}
	
	public void setX(float x){
		view.setX(x);
	}
	
	public void setY(float y){	
		view.setY(y);
	}
	
	public float getX(){
		return view.getX();
	}

	public float getY(){
		return view.getY();
	}
	
	
	
	public float[] getCurrentGrid()
	{
			this.currentGrid = new float[] {view.getX(), view.getY()};
			//Log.v("puzzleDebug", "CurrentGrid: "+ currentGrid[0] + ", " + currentGrid[1]);
			return currentGrid;
			
	}
	
	public int getCurrentID(){
		if(view.getX() == 0 && view.getY() == 0)
		{
			this.currentID = 1;
		}
		
		else if(view.getX() == 160 && view.getY() == 0)
		{
			this.currentID = 2;
		}
		
		else if(view.getX() == 320 && view.getY() == 0)
		{
			this.currentID = 3;
		}
		
		else if(view.getX() == 0 && view.getY() == 160)
		{
			this.currentID = 4;
		}
		
		else if(view.getX() == 160 && view.getY() == 160)
		{
			this.currentID = 5;
		}
		
		else if(view.getX() == 320 && view.getY() == 160)
		{
			this.currentID = 6;
		}
		
		else if(view.getX() == 0 && view.getY() == 320)
		{
			this.currentID = 7;
		}
		
		else if(view.getX() == 160 && view.getY() == 320)
		{
			this.currentID = 8;
		}
		
		else if(view.getX() == 320 && view.getY() == 320)
		{
			this.currentID = 9;
		}
		//Log.v("puzzleDebug", "CurrentID: "+currentID);
		
		return this.currentID;
	}
	
	
}
